package control
{
	import flash.events.Event;
	import flash.net.URLVariables;
	
	import model.ModelLocator;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import vo.User;
	public class registerControl
	{
		internal var theModel:ModelLocator = ModelLocator.getInstance();
		
		private var registerHttp:HTTPService;
		public function registerControl()
		{
			registerHttp = new HTTPService();
			registerHttp.url = "http://localhost:8080/GMdb/servlet/Register";
			registerHttp.addEventListener(ResultEvent.RESULT,registerResultHandler);
			registerHttp.addEventListener(FaultEvent.FAULT, registerFaultHandler);
		}
		public function doRegister(username:String,password:String,city:String,job:String,email:String,sex:String,ifview:String,age:String)
	
		{
			var params:URLVariables = new URLVariables();
       
            params.username = username;
            params.password = password;
            params.city = city;
            params.job = job;
            params.email = email;
            params.sex = sex;
            params.ifview = ifview;
           //Alert.show(age);
            params.age = age;
            registerHttp.send(params); 
		}
		private function httpEncoding(param:String):String{
                return encodeURIComponent(param);
        }
		internal function registerResultHandler(event:ResultEvent):void{
			if(event.result.Result.STATUS == "Successful"){
         			//Alert.show("here");
         			
         			
         			theModel.dispatchEvent(new Event(ModelLocator.REGISTER_OK));
         	}
         	else if(event.result.Result.STATUS == "Existed Username"){
         		//Alert.show("here");
         		theModel.dispatchEvent(new Event(ModelLocator.NAME_EXISTED));
         	}
         	else{
         		
         			theModel.dispatchEvent(new Event(ModelLocator.REGISTER_FAILED));
         		}
		}
		internal function registerFaultHandler(event:FaultEvent):void{
			Alert.show(event.fault.message,"Error");
		}

	}
}